package com.iotek.ssm.controller;

import com.iotek.ssm.pojo.ApplySheet;
import com.iotek.ssm.pojo.ApplySheetQueryVo;
import com.iotek.ssm.pojo.CustomQueryVo;
import com.iotek.ssm.pojo.PageData;
import com.iotek.ssm.service.ApplyService;
import com.iotek.ssm.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/apply")
public class ApplyController {

    @Autowired
    private ApplyService applyService;

    @Autowired
    private Util util;

    @RequestMapping("/goApplyManage")
    public String applyManage() {
        return "admin/applyManage";
    }

    //所有的应聘消息
//    @ModelAttribute("allApplySheets")
//    public List<ApplySheet> allApplySheets() {
//        List<ApplySheet> applySheets = null;
//        try {
//            applySheets = applyService.findAllApply();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return applySheets;
//    }

    //查询条件：未查看的应聘信息
    @ModelAttribute("applyNotLook")
    public List<ApplySheet> applyNotLook() {
        return util.getApplyNotLook();
    }


    //查询条件：已查看未安排的应聘信息
    @ModelAttribute("applyNotDeal")
    @ResponseBody
    public List<ApplySheet> applyNotDeal() {
        ApplySheetQueryVo asq = new ApplySheetQueryVo();
        asq.setLooked(1);  // 条件：已查看
        asq.setArranged(0);// 条件：未安排面试
        List<ApplySheet> applySheets = null;
        try {
            applySheets = applyService.findApplyList(asq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return applySheets;
    }

    @RequestMapping("/updateApply")
    public String updateApply(Model model, ApplySheet applySheet) {
        try {
            applyService.updateApply(applySheet);
            util.setJumpDetails(model, "更新成功", "返回首页！", "../admin/managePageJump", 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "jumpPage";
    }

    @RequestMapping("/addInterviewDate")
    public String addInterviewDate(Model model, ApplySheet applySheet) {
        applySheet.setLooked(1);
        try {
            applyService.updateApply(applySheet);
            util.setJumpDetails(model, "消息发布成功", "返回首页！", "../admin/managePageJump", 1);
        } catch (Exception e) {
            e.printStackTrace();
            util.setJumpDetails(model, "消息发布失败", "返回首页！", "../admin/managePageJump", 1);
        }
        return "jumpPage";
    }

    @RequestMapping("/getApplyPageData")
    @ResponseBody
    public PageData<ApplySheet> getApplyPageData(Integer start) {
        CustomQueryVo customQueryVo = new CustomQueryVo();
        Integer pageSize = PageData.PAGE_SIZE;
        customQueryVo.setCounts(pageSize);
        customQueryVo.setStart((start - 1) * pageSize);
        PageData<ApplySheet> applyPageData = new PageData<ApplySheet>();
        try {
            Integer totalPages = getTotalPages();
            List<ApplySheet> applySheets = applyService.findAllApplyLimit(customQueryVo);
            applyPageData.setTotalPages(totalPages);
            applyPageData.setList(applySheets);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return applyPageData;
    }

    @RequestMapping("/getTotalPages")
    @ResponseBody
    public Integer getTotalPages() {
        Integer totalPages = null;
        try {
            Integer total = applyService.findAllApplyCounts();
            Integer size = PageData.PAGE_SIZE;
            if (total % size != 0)
                totalPages = total / size + 1;
            else
                totalPages = total / size;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return totalPages;
    }
}
